package Remote;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import DbObjects.Person;
import MockDb.IMockDb;

@SuppressWarnings("serial")
public class PersonService extends UnicastRemoteObject implements ISimpleService<Person>
{
	private IMockDb<Person> _db;
	
	public PersonService(IMockDb<Person> mockDb) throws RemoteException
	{
		this._db = mockDb;
		mockDb.Initialize();
		System.out.println("PersonService: Database initialized");
	}
	
	@Override
	public Person Read(int id)
	{
		System.out.println("PersonService: Received request for id " + id);
		
		return this._db.Get(id);
	}

	@Override
	public boolean Update(Person itemToUpdate) 
	{
		System.out.println("PersonService: Received request for updating person " + itemToUpdate.toString());
		boolean success = true;
		
		try
		{
			this._db.Update(itemToUpdate);
		}
		catch(Exception ex)
		{
			success = false;
		}
		
		System.out.println("PersonService: Updated status is: " + success);
		
		return success;
	}

	@Override
	public Iterable<Person> List() 
	{
		System.out.println("PersonService: Received request for list");
		return this._db.GetAll();
	}
}
